package com.cyf.smartCity.service.impl;

import com.cyf.db.bean.*;
import com.cyf.db.mapper.PayOrderMapper;
import com.cyf.db.mapper.RedPacketRecordMapper;
import com.cyf.db.mapper.smartCity.*;
import com.cyf.smartCity.bean.*;
import com.cyf.smartCity.service.LocalCarWarningService;
import com.cyf.smartCity.service.RecordSummaryService;
import com.cyf.util.CommonRsp;
import com.cyf.util.DateUtil;
import com.cyf.util.ImageUtil;
import com.cyf.util.PageInfoBT;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class RecordSummaryServiceImpl implements RecordSummaryService {
    @Autowired
    private LocalInOutParkingMapper localInOutParkingMapper;
    @Autowired
    private LocalDeviceMapper localDeviceMapper;
    @Autowired
    private LocalFeeDetailMapper localFeeDetailMapper;
    @Autowired
    private PayOrderMapper payOrderMapper;
    @Autowired
    private ImageUtil imageUtil;
    @Autowired
    private LocalInParkingMapper localInParkingMapper;
    @Autowired
    private LocalFeeGroupMapper localFeeGroupMapper;
    @Autowired
    private LocalRechargeRecordMapper localRechargeRefundMapper;
    @Autowired
    private LocalCarWarningMapper localCarWarningMapper;
    @Autowired
    private LocalCarWarningService localCarWarningService;
    @Autowired
    private RedPacketRecordMapper redPacketRecordMapper;

    @Override
    public WechatParkListInOutParkingRsp listInOutParking(WechatParkListInOutParkingReq req) {
        WechatParkListInOutParkingRsp rsp = new WechatParkListInOutParkingRsp();

        List<InOutParking> inOutParkingList = localInOutParkingMapper.listByCondition(req.getParkingId(), null, req.getStartTime(), req.getEndTime(), req.getPlate(), req.getPageNum() * req.getPageSize(), req.getPageSize());
        if (CollectionUtils.isNotEmpty(inOutParkingList)) {
            List<WechatParkListInOutParkingRsp.InOutParking> list = new ArrayList<>();
            for (InOutParking inOutParking : inOutParkingList) {
                WechatParkListInOutParkingRsp.InOutParking item = new WechatParkListInOutParkingRsp.InOutParking();
                item.setPlate(inOutParking.getCarLicense());
                if (inOutParking.getPrimaryInTime() != null) {
                    item.setInTime(DateUtil.convertDate2Str(inOutParking.getPrimaryInTime(), DateUtil.FORMAT_YYYY_MM_DD_HH_MI_SS));
                }
                item.setOutTime(DateUtil.convertDate2Str(inOutParking.getPrimaryOutTime(), DateUtil.FORMAT_YYYY_MM_DD_HH_MI_SS));
                if (StringUtils.isNotEmpty(inOutParking.getPrimaryInDeviceName())) {
                    item.setInDeviceName(inOutParking.getPrimaryInDeviceName());
                } else if (inOutParking.getPrimaryInDeviceId() != null) {
                    item.setInDeviceName(localDeviceMapper.getNameByParkingIdAndLocalDeviceId(inOutParking.getParkingId(), inOutParking.getPrimaryInDeviceId()));
                }

                if (StringUtils.isNotEmpty(inOutParking.getPrimaryOutDeviceName())) {
                    item.setOutDeviceName(inOutParking.getPrimaryOutDeviceName());
                } else if (inOutParking.getPrimaryOutDeviceId() != null) {
                    item.setOutDeviceName(localDeviceMapper.getNameByParkingIdAndLocalDeviceId(inOutParking.getParkingId(), inOutParking.getPrimaryOutDeviceId()));
                }

                item.setId(inOutParking.getId());
                item.setInImageUrl(imageUtil.getCarImgHttpUrl(inOutParking.getPrimaryInImage(), inOutParking.getParkingId()));
                item.setOutImageUrl(imageUtil.getCarImgHttpUrl(inOutParking.getPrimaryOutImage(), inOutParking.getParkingId()));

                List<FeeDetail> feeDetailList = localFeeDetailMapper.listFeeByInoutParkingId(inOutParking.getLocalInoutparkingId(), inOutParking.getParkingId());
                if (CollectionUtils.isNotEmpty(feeDetailList)) {
                    item.setActualFee(localFeeDetailMapper.getSumActualFeeAndWechatFee(inOutParking.getParkingId(), inOutParking.getLocalInoutparkingId()));
                    String payWayDesc = "现金支付";
                    for (FeeDetail feeDetail : feeDetailList) {
                        // 在线支付的
                        if (feeDetail.getWechatFee() > 0) {
                            if (feeDetail.getActualfee() > 0) {
                                payWayDesc = "混合支付";
                                break;
                            }
                            List<Integer> payWayList = payOrderMapper.listDistinctPayWayByFeeDetailId(feeDetail.getLocalFeedetailId(), inOutParking.getParkingId());
                            if (CollectionUtils.isNotEmpty(payWayList)) {
                                if (payWayList.size() > 1) {
                                    payWayDesc = "混合支付";
                                    break;
                                }
                                payWayDesc = PayOrder.getPayWayDesc(payWayList.get(0));
                                break;
                            }
                        }
                    }
                    item.setPayWay(payWayDesc);
                }
                list.add(item);
            }

            rsp.setInOutParkingList(list);
        }
        rsp.setResult(WechatParkListFeeGroupRsp.SUCCESS);
        return rsp;
    }

    @Override
    public WechatParkListInParkingRsp listInParking(WechatParkListInParkingReq req) {
        WechatParkListInParkingRsp rsp = new WechatParkListInParkingRsp();

        List<LocalInParking> inParkingList = localInParkingMapper.listByCondition(req.getParkingId(),
                req.getStartTime(), req.getEndTime(), req.getPlate(), req.getLocalFeegroupId(),
                req.getPageNum() * req.getPageSize(), req.getPageSize());
        if (CollectionUtils.isNotEmpty(inParkingList)) {
            List<WechatParkListInParkingRsp.InParking> list = new ArrayList<>();
            for (LocalInParking inParking : inParkingList) {
                WechatParkListInParkingRsp.InParking item = new WechatParkListInParkingRsp.InParking();
                item.setPlate(inParking.getPlate());
                item.setInTime(DateUtil.convertDate2Str(inParking.getPrimaryInTime(), DateUtil.FORMAT_YYYY_MM_DD_HH_MI_SS));
                item.setInDeviceName(inParking.getPrimaryInDeviceName());
                item.setImageUrl(imageUtil.getCarImgHttpUrl(inParking.getPrimaryInImage(), inParking.getParkingId()));
                LocalFeeGroup localFeeGroup = localFeeGroupMapper.getByParkingIdAndLocalId(req.getParkingId(), inParking.getFeeGroupId());
                item.setCarType(localFeeGroup.getBillingTypeDesc());
                list.add(item);
            }

            rsp.setInParkingList(list);
        }
        rsp.setResult(WechatParkListFeeGroupRsp.SUCCESS);
        return rsp;
    }

    @Override
    public WechatParkListRechargeRefundRsp listRechargeRefund(WechatParkListRechargeRefundReq req) {
        WechatParkListRechargeRefundRsp rsp = new WechatParkListRechargeRefundRsp();

        List<LocalRechargeRecord> rechargeRecordList = localRechargeRefundMapper.listByCondition(req.getParkingId(),
                req.getCreatedTime(), req.getPlate(),
                req.getPageNum() * req.getPageSize(), req.getPageSize());
        if (CollectionUtils.isNotEmpty(rechargeRecordList)) {
            List<WechatParkListRechargeRefundRsp.RechargeRefund> list = new ArrayList<>();
            for (LocalRechargeRecord rechargeRecord : rechargeRecordList) {
                WechatParkListRechargeRefundRsp.RechargeRefund item = new WechatParkListRechargeRefundRsp.RechargeRefund();
                item.setPlate(rechargeRecord.getPlate());
                item.setPersonName(rechargeRecord.getPersonName());
                item.setFeeType(rechargeRecord.getFeeType());
                item.setCreatedTime(DateUtil.convertDate2Str(rechargeRecord.getCreatedTime(), DateUtil.FORMAT_YYYY_MM_DD_HH_MI_SS));
                item.setOriginalMoney(rechargeRecord.getOriginalMoney());
                item.setRechargeMoney(rechargeRecord.getRechargeMoney());
                item.setTotalMoney(rechargeRecord.getTotalMoney());
                if (rechargeRecord.getOldEndDate() != null) {
                    item.setOldEndDate(DateUtil.convertDate2Str(rechargeRecord.getOldEndDate(), DateUtil.FORMAT_YYYY_MM_DD));
                    item.setNewEndDate(DateUtil.convertDate2Str(rechargeRecord.getNewEndDate(), DateUtil.FORMAT_YYYY_MM_DD));
                }
                list.add(item);
            }

            rsp.setRechargeRefundList(list);
        }
        rsp.setResult(WechatParkListFeeGroupRsp.SUCCESS);
        return rsp;
    }

    @Override
    public WechatParkListWarningCarRsp listWarningCar(WechatParkListWarningCarReq req) {
        WechatParkListWarningCarRsp rsp = new WechatParkListWarningCarRsp();

        List<LocalCarWarning> carWarningList = localCarWarningMapper.listByPlateAndParkingId(req.getPlate(), req.getParkingId());
        if (CollectionUtils.isNotEmpty(carWarningList)) {
            List<WechatParkListWarningCarRsp.Info> list = new ArrayList<>();
            for (LocalCarWarning carWarning : carWarningList) {
                WechatParkListWarningCarRsp.Info item = new WechatParkListWarningCarRsp.Info();
                item.setId(carWarning.getId());
                item.setPlate(carWarning.getLicense());
                item.setCreatedTime(DateUtil.convertDate2Str(carWarning.getCreatedTime(), DateUtil.FORMAT_YYYY_MM_DD_HH_MI_SS));
                list.add(item);
            }
            rsp.setWarningCarList(list);
        }

        rsp.setResult(WechatBasicRsp.SUCCESS);
        return rsp;
    }

    @Override
    public WechatParkBasicRsp addWarningCar(WechatParkAddWarningCarReq req) {
        WechatParkBasicRsp rsp = new WechatParkBasicRsp();

        LocalCarWarning carWarning = new LocalCarWarning();
        carWarning.setParkingId(req.getParkingId());
        carWarning.setLicense(req.getPlate());
        carWarning.setCreatedUserId(req.getLocalManageaccountId());
        carWarning.setCreatedTime(new Date());
        CommonRsp commonRsp = localCarWarningService.addWarningCar(carWarning);
        if (!commonRsp.isOk()) {
            rsp.setResult(WechatBasicRsp.FAIL);
            rsp.setStrError(commonRsp.getStrError());
            return rsp;
        }
        rsp.setResult(WechatBasicRsp.SUCCESS);
        return rsp;
    }

    @Override
    public WechatParkBasicRsp editWarningCar(WechatParkEditWarningCarReq req) {
        WechatParkBasicRsp rsp = new WechatParkBasicRsp();

        CommonRsp commonRsp = localCarWarningService.editWarningCar(req.getId(), req.getPlate());
        if (!commonRsp.isOk()) {
            rsp.setResult(WechatBasicRsp.FAIL);
            rsp.setStrError(commonRsp.getStrError());
            return rsp;
        }
        rsp.setResult(WechatBasicRsp.SUCCESS);
        return rsp;
    }

    @Override
    public WechatParkBasicRsp deleteWarningCar(Integer id) {
        WechatParkBasicRsp rsp = new WechatParkBasicRsp();

        localCarWarningService.deleteById(id);

        rsp.setResult(WechatBasicRsp.SUCCESS);
        return rsp;
    }

    @Override
    public RedPacketRecordRsp redPacketRecordList(RedPacketReq req) {
        RedPacketRecordRsp rsp = new RedPacketRecordRsp();
        try {
            PageHelper.startPage(req.getPage(),req.getPageSize());
            List<RedPacketRecord> redPacketRecords = redPacketRecordMapper.listRedPacketRecord(req);
            PageInfoBT pib = new PageInfoBT(redPacketRecords);
            rsp.setData(pib);
            rsp.setResult(WechatBasicRsp.SUCCESS);
        }catch (Exception e){
            rsp.setResult(WechatBasicRsp.FAIL);
            log.error("获取红包发放记录列表异常{}",e.getMessage());
        }
        return rsp;
    }

    @Override
    public RedPacketRechargeRsp redPacketRechargeList(RedPacketReq req) {
        RedPacketRechargeRsp rsp = new RedPacketRechargeRsp();
        try {
            PageHelper.startPage(req.getPage(),req.getPageSize());
            List<LocalIctrechargerecord> ictrechargerecordList = redPacketRecordMapper.redPacketRechargeList(req);
            for (LocalIctrechargerecord localIctrechargerecord : ictrechargerecordList) {
                if(localIctrechargerecord.getCreatedTime()!= null){
                    Date date = DateUtil.parseDate(localIctrechargerecord.getCreatedTime(), "yyyy-MM-dd HH:mm:ss");
                    localIctrechargerecord.setCreatedTime(DateUtil.convertDate2Str(date,"yyyy-MM-dd HH:mm:ss"));
                }
            }
            PageInfoBT pib = new PageInfoBT(ictrechargerecordList);
            rsp.setData(pib);
            rsp.setResult(WechatBasicRsp.SUCCESS);
        }catch (Exception e){
            rsp.setResult(WechatBasicRsp.FAIL);
            log.error("获取红包充值记录列表异常{}",e.getMessage());
        }
        return rsp;
    }
}
